package com.tantinevincent.normalization;

import java.util.Arrays;

import com.tantinevincent.vector.DenseVector;
import com.tantinevincent.vector.IVector;

public class MinMaxNormalizar implements INormalizar{
	
	private double[] mins;
	private double[] maxs;
	
	public MinMaxNormalizar(double[] mins,double[] maxs){
		this.mins = mins;
		this.maxs = maxs;
	}
	
	public MinMaxNormalizar(int size){
		mins = new double[size];
		maxs = new double[size];
		reset();
	}
	public void load(IVector v){
		for( int index : v.getIndices() ){
			double value = v.get(index);
			// check max value
			if( value > maxs[index] ){
				maxs[index] = value;
			}
			// check min value
			if( value < mins[index] ){
				mins[index] = value;
			}
		}
	}
	public IVector normalize(IVector v){
		DenseVector normalizatedVector = new DenseVector( v.length() );
		for( int index : v.getIndices() ){
			double originValue = v.get(index);
			double value;
			if( maxs[index] == mins[index] ){
				value = originValue;
			}else{
				value = (originValue - mins[index])/(maxs[index] - mins[index]);
			}
			//double value = (originValue - mins[index])/(maxs[index] - mins[index]);
			normalizatedVector.set(index, value);
		}
		return normalizatedVector;
	}
	public double normalize( int index,double value ){
		double normalizatedValue;
		if( maxs[index] == mins[index] ){
			normalizatedValue = value;
		}else{
			normalizatedValue = (value - mins[index])/(maxs[index] - mins[index]);
		}
		return normalizatedValue;
	}
	public double[] getMins(){
		return mins;
	}
	public double[] getMaxs(){
		return maxs;
	}
	
	
	public void reset(){
		Arrays.fill(mins, Double.MAX_VALUE);
		Arrays.fill(maxs, -Double.MAX_VALUE);
	}
}
